package com.platform.usercenter.service.impl;
import com.platform.aropenapi.api.remote.api.RemoteArOpenApiService;
import com.platform.common.core.constant.ServiceNameConstants;
import com.platform.common.core.domain.R;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.usercenter.config.ConfigConstants;
import com.platform.usercenter.mapper.SysUserMapper;
import com.platform.usercenter.service.RokidService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class RokidServiceImpl implements RokidService {

    @Resource
    ConfigConstants configConstants;

    @Resource
    SysUserMapper userMapper;

    @Resource
    RemoteArOpenApiService remoteArOpenApiService;


    @Override
    public void registerAllUserToRokid(){

    }

    @Override
    public void registerBatchUsers(Long[] userIds) throws Exception {
        List<SysUser> list=userMapper.selectUserByIds(userIds);
        int pages=list.size()/10;
        int flag=list.size()%10;
        if(flag!=0){
            pages=pages+1;
        }
        for(int i=1;i<=pages;i++){
            int endindex=10*i;
            if(endindex>list.size()){
                endindex=list.size();
            }
            List<SysUser> smlist=list.subList(10*(i-1),endindex);
            R r=remoteArOpenApiService.registerUserToRokid(smlist, ServiceNameConstants.USER_CENTER_SERVICE);
            if(r.getCode()!=200){
                throw new Exception(r.getMsg());
            }
        }
    }

    @Override
    public void updateUsersToRokid(Long[] userIds) {
        List<SysUser> list=userMapper.selectUserByIds(userIds);
        int pages=list.size()/10;
        int flag=list.size()%10;
        if(flag!=0){
            pages=pages+1;
        }
        for(int i=1;i<=pages;i++){
            int endindex=10*i;
            if(endindex>list.size()){
                endindex=list.size();
            }
            List<SysUser> smlist=list.subList(10*(i-1),endindex);
            remoteArOpenApiService.updateUsersToRokid(smlist, ServiceNameConstants.USER_CENTER_SERVICE);
        }
    }

    @Override
    public void deleteRokidUsers(Long[] userIds) throws Exception {
        List<SysUser> list=userMapper.selectUserByIds(userIds);
        int pages=list.size()/10;
        int flag=list.size()%10;
        if(flag!=0){
            pages=pages+1;
        }
        for(int i=1;i<=pages;i++){
            int endindex=10*i;
            if(endindex>list.size()){
                endindex=list.size();
            }
            List<SysUser> smlist=list.subList(10*(i-1),endindex);
            R r=remoteArOpenApiService.deleteRokidUsers(smlist, ServiceNameConstants.USER_CENTER_SERVICE);
            if(r.getCode()!=200){
                throw new Exception(r.getMsg());
            }
        }
    }
}
